import algorithms.SortAlgorithm;
import team_leader.BubbleSort;


import java.util.HashMap;
import java.util.Map;

public class Main {
    private static final Map<String, SortAlgorithm> ALGORITHM_MAP = new HashMap<>();

    static {
        // 注册所有可用算法
        registerAlgorithm(new BubbleSort());
    }

    private static void registerAlgorithm(SortAlgorithm algorithm) {
        ALGORITHM_MAP.put(algorithm.getAlgorithmName().toLowerCase(), algorithm);
    }

    public static void main(String[] args) {
        int[] arr = {64, 34, 25, 12, 22, 11, 90};

        if (args.length > 0) {
            // 命令行模式运行指定算法
            runSpecificAlgorithm(args[0], arr);
        } else {
            // 无参数时默认运行所有算法
            runAllAlgorithms(arr);
        }
    }

    private static void runSpecificAlgorithm(String algoName, int[] arr) {
        SortAlgorithm algorithm = ALGORITHM_MAP.get(algoName.toLowerCase());
        if (algorithm != null) {
            printResult(algorithm, arr.clone());
        } else {
            System.err.println("未找到算法: " + algoName);
            System.out.println("可用算法列表：");
            ALGORITHM_MAP.keySet().forEach(System.out::println);
        }
    }

    private static void runAllAlgorithms(int[] arr) {
        ALGORITHM_MAP.values().forEach(algorithm ->
                printResult(algorithm, arr.clone())
        );
    }

    private static void printResult(SortAlgorithm algorithm, int[] arr) {
        algorithm.sort(arr);
        System.out.println("开发者：" + algorithm.getDeveloperName());
        System.out.println("算法名称：" + algorithm.getAlgorithmName());
        System.out.print("排序结果：");
        for (int num : arr) {
            System.out.print(num + " ");
        }
        System.out.println("\n----------------------");
    }
}